﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class TurnAndPlayerVariables : MonoBehaviour
{
	private bool isPlayerTurn;
	
	private List<string> shipNames;
	private Dictionary<string, bool> isShipSelected;
	private Dictionary<string, bool> isShipAlive;
	private Dictionary<string, bool> isShipAttacking;

	
	void Start()
	{
		isPlayerTurn = true;
		
		shipNames = new List<string>();
		isShipSelected = new Dictionary<string, bool>();
		isShipAlive = new Dictionary<string, bool>();
		isShipAttacking = new Dictionary<string, bool>();
	}
	
	public bool PlayerTurn
	{
		get { return isPlayerTurn; }
		set { isPlayerTurn = value; }
	}
	
	public bool EnemyTurn
	{
		get { return !isPlayerTurn; }
		set { isPlayerTurn = !value; }
	}
	
	public void AddShipToList(string name)
	{
		shipNames.Add(name);
		isShipSelected.Add(name, false);
		isShipAlive.Add(name, true);
		isShipAttacking.Add(name, false);
	}
	
	public bool IsShipAlive(string name)
	{
		if (shipNames.Contains(name))
			return isShipAlive[name];
		else
			return false;;
	}
	
	public bool IsShipSelected(string name)
	{
		if (shipNames.Contains(name))
			return isShipSelected[name];
		else
			return false;
	}
	
	public bool IsShipAttacking(string name)
	{
		if (shipNames.Contains(name))
			return isShipAttacking[name];
		else
			return false;
	}
	
	public void SetSelectedShip(string name)
	{
		foreach (string other in shipNames)
		{
			isShipSelected[other] = false;
		}
		
		if (shipNames.Contains(name))
			isShipSelected[name] = true;

	}
	
	public void SetAttackingShip(string name)
	{
		foreach (string other in shipNames)
		{
			isShipAttacking[other] = false;
		}
		
		if (shipNames.Contains(name))
			isShipAttacking[name] = true;

	}
	
	public void DeselectAll()
	{
		foreach (string other in shipNames)
		{
			isShipSelected[other] = false;
		}
	}
	
	public void DeselectShip(string name)
	{
		if (shipNames.Contains(name))
			isShipSelected[name] = false;
	}
	
	public bool AnySelected()
	{
		bool anySelected = false;
		
		foreach (string other in shipNames)
		{
			anySelected = anySelected || isShipSelected[other];
		}
		
		return anySelected;
	}
	
	public bool AnyAttacking()
	{
		bool anyAttacking = false;
		
		foreach (string other in shipNames)
		{
			anyAttacking = anyAttacking || isShipSelected[other];
		}
		
		return anyAttacking;
	}
	
}
